<?php
        $buyer_id = $data[$root]['buyer-id']['VALUE'];
        $google_order_number = $data[$root]['google-order-number']['VALUE'];
        $fulfillment_order_state = $data[$root]['fulfillment-order-state']['VALUE']; //new
        
        $financial_order_state = $data[$root]['financial-order-state']['VALUE']; //reviewing
        $timestamp = $data[$root]['timestamp']['VALUE'];
		$customerID = null;
        if (isset ($data[$root]['shopping-cart']['merchant-private-data'])) {
        	$customerID = $data[$root]['shopping-cart']['merchant-private-data']['customer-id']['VALUE'];
        }

		
        $shippingName = $data[$root]['order-adjustment']['shipping']['flat-rate-shipping-adjustment']['shipping-name']['VALUE'];
        $shippingCost = $data[$root]['order-adjustment']['shipping']['flat-rate-shipping-adjustment']['shipping-cost']['VALUE'];
        log1(" shippingName:" . $shippingName);
		log1("  shippingCost:" . $shippingCost);
		$shippingContactName = $data[$root]['buyer-shipping-address']['contact-name']['VALUE'];
        $shippingCompanyName = $data[$root]['buyer-shipping-address']['company-name']['VALUE'];
        $shippingEmail = $data[$root]['buyer-shipping-address']['email']['VALUE'];
        $shippingPhone = $data[$root]['buyer-shipping-address']['phone']['VALUE'];
        $shippingAddress1 = $data[$root]['buyer-shipping-address']['address1']['VALUE'];
        $shippingAddress2 = $data[$root]['buyer-shipping-address']['address2']['VALUE'];
        $shippingTown = $data[$root]['buyer-shipping-address']['city']['VALUE'];
        $shippingCounty = $data[$root]['buyer-shipping-address']['region']['VALUE'];
        $shippingPostcode = $data[$root]['buyer-shipping-address']['postal-code']['VALUE'];
        $shippingFirstname = $data[$root]['buyer-shipping-address']['structured-name']['first-name']['VALUE'];
        $shippingSurname = $data[$root]['buyer-shipping-address']['structured-name']['last-name']['VALUE'];
        
		$billingContactName = $data[$root]['buyer-billing-address']['contact-name']['VALUE'];
        $billingCompanyName = $data[$root]['buyer-billing-address']['company-name']['VALUE'];
        $billingEmail = $data[$root]['buyer-billing-address']['email']['VALUE'];
        $billingPhone = $data[$root]['buyer-billing-address']['phone']['VALUE'];
        $billingAddress1 = $data[$root]['buyer-billing-address']['address1']['VALUE'];
        $billingAddress2 = $data[$root]['buyer-billing-address']['address2']['VALUE'];
        $billingTown = $data[$root]['buyer-billing-address']['city']['VALUE'];
        $billingCounty = $data[$root]['buyer-billing-address']['region']['VALUE'];
        $billingPostcode = $data[$root]['buyer-billing-address']['postal-code']['VALUE'];
        $billingFirstname = $data[$root]['buyer-billing-address']['structured-name']['first-name']['VALUE'];
        $billingSurname = $data[$root]['buyer-billing-address']['structured-name']['last-name']['VALUE'];
		//ignore this temporarily
        $emailAllowed = $data[$root]['buyer-marketing-preferences']['email-allowed']['VALUE'];

        $orderTotal = $data[$root]['order-total']['VALUE'];
        $shippingAddressID = insertAddress($shippingContactName, $shippingCompanyName, 'D', null, null, null, null, $shippingAddress1, $shippingAddress2, $shippingTown, $shippingPostcode, $shippingCounty, $shippingEmail, $shippingPhone);
        $billingAddressID = insertAddress($billingContactName, $billingCompanyName, 'B', null, null, null, null, $billingAddress1, $billingAddress2, $billingTown, $billingPostcode, $billingCounty, $billingEmail, $billingPhone);
		
		/* google: 2008-03-24T18:15:07.333Z
		mysql:	HH:MM:SS	Y-m-d */		
		$orderDate = substr($timestamp, 0 , 10);
		$orderTime = substr($timestamp, 11 , 8);
		$guaranteedDeliveryDays = 14;
		
		$orderDueDate = strftime("%Y-%m-%d", strtotime("$orderDate + $guaranteedDeliveryDays days"));	       
		insertOrder($google_order_number,$buyer_id,$customerID,$shippingAddressID,$billingAddressID, $orderDate,
			$orderTime, $orderDueDate, null, $orderTotal, $fulfillment_order_state, $financial_order_state, $shippingCost, $shippingName);
		$orderID =  $google_order_number;
        $items = get_arr_result($data[$root]['shopping-cart']['items']['item']);
        foreach ($items as $item) {
        	$item_price = $item['unit-price']['VALUE'];
			$g_garment_id = $item['merchant-private-item-data']['garment']['VALUE'];
			log1("g_garment_id=$g_garment_id");
			$f_fabric_id =$item['merchant-private-item-data']['fabric']['VALUE'];
        	if ($customerID != null) {
        		$s_size_id = $item['merchant-private-item-data'] ['s_size_id']['VALUE'];
        	} else {
        		$s_name = $item['merchant-private-item-data'] ['s_name']['VALUE'];
        		$s_shoulder = $item['merchant-private-item-data'] ['s_shoulder']['VALUE'];
        		$s_chest = $item['merchant-private-item-data'] ['s_chest']['VALUE'];
        		$s_bust = $item['merchant-private-item-data'] ['s_bust']['VALUE'];
        		$s_waist = $item['merchant-private-item-data'] ['s_waist']['VALUE'];
        		$s_hips = $item['merchant-private-item-data'] ['s_hips']['VALUE'];
        		$s_back_waist =$item['merchant-private-item-data'] ['s_back_waist']['VALUE'];
        		$s_legs = $item['merchant-private-item-data'] ['s_legs']['VALUE'];
        		$s_height = $item['merchant-private-item-data'] ['s_height']['VALUE'];
        		$s_size_id = insertSize($s_name, $s_shoulder, $s_chest, $s_bust, $s_waist, 
					$s_hips, $s_back_waist, $s_leg, $s_height);
        	}
			
			$orderItemID = insertOrderItem($g_garment_id,
						$f_fabric_id,
						$orderID,
						$s_size_id,
						"N",
						$item_price,
						$item_price //$oi_real_cost
						);
			$garment = getGarment($g_garment_id);
			$fabric = getFabric($f_fabric_id);
			$employees = getAllEmployees();
			shuffle($employees); //to make work allocation fair
			for ($workDay = strftime("%Y-%m-%d", strtotime("$orderDate + 1 day")) ; 
				strcmp($workDay, $orderDueDate) < 0; 
				$workDay = strftime("%Y-%m-%d", strtotime("$workDay + 1 day"))  ) {
				//check if weekend
				log1("Workday:$workDay");
				$dayOfWeek =   strftime("%w", strtotime("$workDay"));
				if ($dayOfWeek == 0 || $dayOfWeek ==6) {
					continue;
				}
				//check if bank holiday
				$isaBankHoliday = isBankHoliday($workDay);
				if ($isaBankHoliday) {
					continue;
				}
				foreach($employees as $employee) {
					$employeeID = $employee['e_employee_id'];
					if (allocateWork($employeeID , $workDay , $orderItemID,   $garment['g_hours_to_make'], $employee['e_hours_per_day'])) {
						break 2;
					}
				}
			}
        }

?>
